<?php
namespace app\admin\controller;
use app\service\communityService;
use app\service\propertyZftService;
use app\service\unitService;
use think\facade\Db;
use think\facade\View;

ini_set('max_execution_time','0');
class Unit extends Common{

    public function list(){
        //查询服务商数据条数
        $com = $this->pidTokenId();
        //$where['id'] = array('in',$com['property_id']);
        $where[]=['community_id','in',$com['com_id']];
        //查询服务商信息
        $data = request()->get();

        $where = array_filter($where);
        $pagelist = (new unitService())->getList($where);
        $number = (new unitService())->number($where);
        View::assign('number',$number);
        if(!empty($number)){
//            foreach($prolist as $lis){
//                $id = $lis['property_id'];
//                $proname = (new propertyService())->propertyId($id);
//                $lis['property_name'] = $proname['name'];
//                $list[] = $lis;
//                View::assign('list',$list);
//            }
            View::assign('list',$pagelist);
        }else{
            View::assign('list',$pagelist);
        }
        View::assign('pagelist',$pagelist);
        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }

        return View::fetch('list');
    }

    //添加
    public function add(){
        //查询服务商信息
        $com = $this->pidTokenId();
        $where[]=['id','in',$com['com_id']];
        $communitylist =(new communityService())->communityList($where);
        View::assign('communitylist',$communitylist);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //var_dump($data);
//            //判断账号名称是否重复
            $info = Db::table("unit")
                ->where("community_id",$data["community_id"])
                ->where("unit",$data["unit"])
                ->where("group",$data["group"])
                ->find();
            if($info){
                $this->error('该单元重复已添加已存在');
            }

            $community_info=Db::table("community") ->where("id",$data["community_id"])->find();
            //插入数据到数据库S
            $data["community_name"]=$community_info["community_name"];
            $result = (new unitService())->add($data);
            if($result){
                return "添加成功";
                $this->success('添加成功',url('list'));
            }else{
                $this->error('添加物业失败',url('propertyAdd'));
            }
        }
        return View::fetch('add');
    }
    //修改
    public function edit(){
        //查询服务商信息

        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('物业不存在');
            }
            $info=Db::table("property_zftset")->where("id",$id)->find();
            $propertyinfo = (new propertyService())->propertyId($info["property_id"]);

            View::assign('propertyinfo',$propertyinfo);
            View::assign('info',$info);
            return View::fetch('propertyzft/edit');
        }else{
            $data = request()->post();
            $result = (new propertyZftService())->edit($data);
            if($result){
                return "修改成功";
                $this->success('修改物业成功',url('propertyList'));
            }else{
                $this->error('修改物业失败');
            }
        }
        return View::fetch('edit');
    }
}